<template lang="jade">
  div.return-bar
    i.iconfont(@click="back()") &#xe668;
    用户登录
  div.banner.detail_top
    img(src="../../assets/images/logo.png")
  div.login-from
    validator(name="validation1")
      form(v-on:submit.prevent="login")
        div.login-control
          div.control
            span 手机号
            input(v-model="username",placeholder="请输入手机号",v-validate:username="{required:true, maxLength: 10}",maxLength="11")
          div.control
            span 密码
            input(v-show=" !showPWD ", v-model="password",type="password",placeholder="登录密码",,v-validate:password="{required:true}")
            input(v-show=" showPWD ",v-model="password",type="text",placeholder="登录密码",,v-validate:password="{required:true}")
            i.iconfont.icon-eyesclose(@click="switchKw()") {{{iconString}}}

          div.control
            span 图片验证码
            input(v-model="verify",placeholder="请输入图片验证码",v-validate:code="{required:true, maxLength: 4}",maxLength="4")
            img.verify(v-show="show_img", src="{{img_src}}", @click="reloadImg")


          ul.cbox
            li
              input.p_btn.postIt(type="submit", value="登录" )

          div.link
            a.relation-link(v-link="{ path: '/forget-pwd' }") 忘记密码
            span.spli |
            a.relation-link.red(v-link="{ path: '/reg' }") 立即注册
</template>

<script>
import '../../assets/sass/user/login.scss'
import {c_reg_verify_img, Api_Login} from '../../config/api'
var time_ment = null;
export default {
  data () {
    return {
      from: '',
      username: localStorage.getItem("tel") || '',
      password: '',
      itype:'password',
      loginType:'',
      showPWD:false,
      iconString:'&#xe712;',
      img_src:'', 
      show_img:true,
      verify:''
    }
  },
  methods: {
    reloadImg(){
      /*this.$http({url: c_img_verify, method: 'GET'}).then(function (response) {
        this.img_src = c_img_verify;
      });*/
      clearInterval(time_ment);
      this.show_img = false;
      this.img_src = '';
      var that = this;
      var timestamp = Date.parse(new Date());
      timestamp = timestamp / 1000;
      var rkey = that.global.r_key()
      var pkey = that.global.p_key()
      //this.img_src = c_img_verify
      time_ment = setInterval(function(){
        that.show_img = true;
        that.img_src = c_reg_verify_img+'&t='+timestamp+'&r_key='+rkey+'&p_key='+pkey
      },300); 
      
    },


    switchKw(){
      if(this.iconString == '&#xe712;')
        this.iconString = '&#xe648;'
      else
        this.iconString = '&#xe712;'
      this.showPWD = !this.showPWD;
    },
    back(){
      //window.history.back();
      this.$route.router.go('/')
    },

 

    submitFrm(key){
      this.loginType = key;
      this.login();
    },


    login: function () {

      if (this.$validation1.invalid) {
        return false;
      }

      var username = this.username;
      var password = this.password;
      var data = {'verify':this.verify, 'username': username, 'password': password}
      this.$http.post(Api_Login, data,'').then(function (response) {
          var rs = response.data;
          if(!rs.status){
            alert(rs.msg)
            this.reloadImg();
            this.verify='';
          }else {

            localStorage.setItem("tel", username);
            sessionStorage.setItem("login", true);

            /*if(this.loginType=='0')
              sessionStorage.setItem("loan", false)
            else
              sessionStorage.setItem("loan", true)*/

            if(!this.from){
              this.$route.router.go('/')
            }else {
              this.$route.router.go('/'+this.from)
            }
            
          }
      }, function (response) {
          
      });
    }
  },
  route: {
    activate ( transition ) {


      var timestamp = Date.parse(new Date());
      timestamp = timestamp / 1000;

      var rkey = this.global.r_key()
      var pkey = this.global.p_key()

      //this.img_src = c_reg_verify_img+'&t='+timestamp
      this.img_src = c_reg_verify_img+'&t='+timestamp+'&r_key='+rkey+'&p_key='+pkey;

      this.from = transition.to.query.from
      //console.log(this.from)
      if(sessionStorage.getItem("login")){
        this.$route.router.go('/')
      }else{
        transition.next();
      }


      
    }
  }
}
</script>